package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public Result send(Map map) throws Exception {
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if(orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if(orderSetting.getNumber()<=orderSetting.getReservations()){
                return new Result(false,MessageConstant.ORDER_FULL);
            }
        String  telephone = (String) map.get("telephone");
       // String telephone1= RedisMessageConstant.SENDTYPE_LOGIN+telephone;
        Member member = memberDao.findByTelephone(telephone);
        if(member==null){
                    member = new Member();
                    member.setName((String) map.get("name"));
                    member.setPhoneNumber(telephone);
                    member.setIdCard((String) map.get("idCard"));
                    member.setSex((String) map.get("sex"));
                    member.setRegTime(new Date());
                    memberDao.add(member);//自动完成会员注册

                }else{
                    //判断是否在重复预约
                    Integer memberId = member.getId();//会员ID
                    Date order_Date = DateUtils.parseString2Date(orderDate);//预约日期
                    String setmealId = (String) map.get("setmealId");//套餐ID
                    Order order = new Order(memberId, order_Date, Integer.parseInt(setmealId));
                    //根据条件进行查询
                    List<Order> list = orderDao.findByCondition(order);
                    if(list != null && list.size() > 0){
                        //说明用户在重复预约，无法完成再次预约
                        return new Result(false, MessageConstant.HAS_ORDERED);
                    }

                }
                Order order = new Order();
                order.setMemberId(member.getId());//设置会员ID
                order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
                order.setOrderType((String) map.get("orderType"));//预约类型
                order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
                order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
                orderDao.add(order);
                orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
                orderSettingDao.editReservationsByOrderDate(orderSetting);
                return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());




    }

    @Override
    public Result findById(Integer id) {
        try {
            Map map = orderDao.findById4Detail(id);

            return new Result(true,"",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"shibaile");
        }
    }


}
